/*
 * Copyright 2007-2008 Martin Konzett <martin.konzett AT gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.toools.gwt.wmc.client.additive.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Collection;
import java.util.List;
import org.toools.gwt.widget.client.cloud.*;
import org.toools.gwt.widget.client.bc.*;
import org.toools.gwt.wmc.client.additive.AdditiveServiceAsync;
import org.toools.gwt.wmc.jpa.Additive;
import org.toools.gwt.wmc.jpa.CompanyPart;

public class AdditiveControl extends BasicControl {
    
    public AdditiveControl(AdditiveServiceAsync service) {
        super(service, new org.toools.gwt.widget.client.util.Account());
        service.getAdditives(new AsyncCallback<Collection<Additive>>() {
                public void onSuccess(Collection<Additive> result) {
                    System.out.println("additives loaded");
                    TagVO[] tags = AdditiveControl.this.getTagVO(result.toArray());
                    AdditiveControl.this.setTags(tags);
                    AdditiveControl.this.clearStatus();
                    AdditiveControl.this.setStatus( AdditiveControl.this.getLoadedMessage());
                    AdditiveControl.this.getCreateList().clear();
                }

                public void onFailure(Throwable t) {
                    AdditiveControl.this.setStatus( "Failure on loading Additives");
                    AdditiveControl.this.getDefaultCreateTogglePanel().setVisible(false);
                    AdditiveControl.this.getCloudFilter().setVisible(false);
                }

                public String getOnFailureMessage(Throwable t) {
                    System.out.println("" + t);
                    return "Error while loading. Contact your administrator: " + new java.util.Date().getTime();
                }
        });
    }

     public AdditiveControl(AdditiveServiceAsync service, CompanyPart cp) {
        super(service,new org.toools.gwt.widget.client.util.Account());
        Collection<Additive> additives = cp.getAdditives();
        if (additives != null) {
            setTags(getTagVO(additives.toArray()));
        }
    }

    public BusinessObjectPanel createBasicPanel(Object o) {
        return new AdditivePanel(o);
    }
    public BasicListener createBasicListener() {
        return new AdditiveListener(this);
    }
    public BasicList createBasicList() {
        AdditiveList acl = new AdditiveList(new TagVO[]{}, this);
        return acl;
    }
    public TagVO getTagVO(Object bp) {
        Additive ad = (Additive) bp;
        TagVO result = new TagVO("" + ad.getId(), ad.getContent(), 0, 0);
        result.setAttachedBusinessObject(ad);
        return result;
    }
    @Override
    public String getFilterMessage() {
        return "search by content";
    }
    @Override
    public String getTitle() {
        return "Additives";
    }
    @Override
    public void addSortItem() {
        getSortListBox().clear();
        getSortListBox().addItem("content");
    }
}